﻿Imports System
Imports System.CodeDom
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Friend NotInheritable Class LogicalExpressionEditor
        Inherits UITypeEditor
        ' Methods
        Public Overrides Function EditValue(ByVal typeDescriptorContext As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal o As Object) As Object
            If (typeDescriptorContext Is Nothing) Then
                Throw New ArgumentNullException("typeDescriptorContext")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Dim obj2 As Object = o
            Dim instance As RuleConditionReference = [TryCast](typeDescriptorContext.Instance,RuleConditionReference)
            If (((instance Is Nothing) OrElse (instance.ConditionName Is Nothing)) OrElse (instance.ConditionName.Length <= 0)) Then
                Throw New ArgumentException(Messages.ConditionNameNotSet)
            End If
            Dim component As Activity = Nothing
            Dim service As IReferenceService = [TryCast](serviceProvider.GetService(GetType(IReferenceService)),IReferenceService)
            If (Not service Is Nothing) Then
                component = [TryCast](service.GetComponent(typeDescriptorContext.Instance),Activity)
            End If
            Dim conditions As RuleConditionCollection = Nothing
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(serviceProvider, Helpers.GetRootActivity(component))
            If (Not definitions Is Nothing) Then
                conditions = definitions.Conditions
            End If
            If ((Not conditions Is Nothing) AndAlso Not conditions.Contains(instance.ConditionName)) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.ConditionNotFound, New Object() { instance.ConditionName }))
            End If
            Me.editorService = DirectCast(serviceProvider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If (Not Me.editorService Is Nothing) Then
                Dim expression As CodeExpression = [TryCast](typeDescriptorContext.PropertyDescriptor.GetValue(typeDescriptorContext.Instance),CodeExpression)
                Try 
                    [Using] dialog As RuleConditionDialog = New RuleConditionDialog(component, expression)
                        If (DialogResult.OK = Me.editorService.ShowDialog(dialog)) Then
                            obj2 = dialog.Expression
                        End If
                    End [Using]
                Catch exception1 As NotSupportedException
                    DesignerHelpers.DisplayError(Messages.Error_ExpressionNotSupported, Messages.ConditionEditor, serviceProvider)
                End Try
            End If
            Return obj2
        End Function

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function


        ' Fields
        Private editorService As IWindowsFormsEditorService
    End Class
End Namespace

